/*
 * =====================================================================================
 *
 *       Filename:  imagerestore.h
 *
 *    Description:  image restoration using MRF
 *
 *        Version:  1.0
 *        Created:  2009年07月07日 10时52分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ying Wang (WY), ywang@nlpr.ia.ac.cn
 *        Company:  Institute of Automation, Chinese Academy of Sciences
 *
 * =====================================================================================
 */
#ifndef IMAGERESTORE_H
#define IMAGERESTORE_H
#include "loopybp.h"
#define DATA_K 800
/** 
 * \brief E(z) = \sum_{i}(z_i-x_i)^2/sigma + \lambda\sum_{(i,j)\in G}g(z_i-z_j)^2
 *
 */
class ImageRestore : public  MRF
{
public:
	ImageRestore(const NCmatrix<double> &image, double sigma, double lambda,double alpha)
		: MRF(image.row(),image.column(),256)
		, m_Image(image)
		, m_sigma(sigma)
		, m_lambda(lambda)
		, m_alpha(alpha)
	{
	}
public:
	inline double dataitem(int pi,int pj,int labelp) const;

	inline double prioritem(int pi,int pj,int qi,int qj, int labelp,int labelq) const;
private:
	NCmatrix<double> m_Image;
	double m_sigma;
	double m_lambda;
	double m_alpha;
	
};

inline double ImageRestore::dataitem(int pi, int pj, int labelp ) const
{
	double loss = SQE(m_Image[pi][pj] - labelp)/m_sigma;
	return loss< DATA_K ? loss : DATA_K;
}
inline double ImageRestore::prioritem(int pi, int pj, int qi, int qj, int labelp, int labelq) const
{
	double loss = SQE(double(labelp-labelq));
	return m_lambda*(loss>m_alpha ? m_alpha : loss);

}
#endif
